#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb


class baseSql:
    def insert_more_string(self, items):
        sql = ''
        if type(items) == list:
            # print items
            listkey = []
            listvalue2 = []
            listvalue3 = []
            num = 0
            for item in items:
                listvalue = []
                listkey = []
                listvalue2.append([])
                for key in item:
                    listkey.append(key)
                    # listvalue.append(item[key])
                    values = MySQLdb.escape_string(str(item[key]))
                    listvalue2[num].append(values)
                num = num + 1
            keys = '(' + ','.join(listkey) + ')'
            for values in listvalue2:
                values2 = []
                for value in values:
                    value = '\'' + str(value) + '\''
                    values2.append(value)
                values = values2
                try:
                    values = '(' + ','.join(values) + ')'
                except:
                    assert ("join err, please check your values is str or other,other will fail")
                listvalue3.append(values)
            values = ','.join(listvalue3)
            sql = keys + ' VALUES ' + values
            return True, sql

        if type(items) == dict:
            sql = self.insert_one_string(items)
            return True, sql
        return False, sql

    def insert_one_string(self, items):
        listkey = []
        listvalue = []
        listvalue2 = []
        for key in items:
            listkey.append(key)
            values = MySQLdb.escape_string(str(items[key]))
            listvalue.append(values)
        keys = '(' + ','.join(listkey) + ')'
        for value in listvalue:
            value = '\'' + str(value) + '\''
            listvalue2.append(value)
        values = '(' + ','.join(listvalue2) + ')'
        sql = keys + ' VALUES ' + values
        # print sql
        return sql
